import os
import sys
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
import pandas as pd
import tushare as ts
from utils.confutils import ConfUtil
from tqdm import tqdm
import datetime
import time
# 提取token
token = ConfUtil().get_tokens('tushare')
pro = ts.pro_api(token)
# 读取上交所交易日历，获得交易日清单
calendar_file = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) + "\\Data\\RawData\\calendar.feather"
df_calendar = pd.read_feather(calendar_file)
tradingdays = df_calendar.query("exchange=='SSE' and is_open=='1'").cal_date.sort_values().unique()

# 取20100101开始每天的st股票清单
df_full = []
print(f'开始下载全市场st股票清单')
for date_ in tqdm(tradingdays):
    while 1==1:
        try:
            df =  pro.stock_st(trade_date=date_)
            if df is None:
                df = pd.DataFrame()
            break
        except:
            time.sleep(5)
            continue
    if not df.empty:
        df_full.append(df)
df_res = pd.concat(df_full,axis=0)
print('下载完毕')
print(f'数据信息：\n{df_res.info()}')
print(df_res.head(5))
# 存到..\Data\RawData\stock_basic.feather文件中
tofile = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) + "\\Data\\RawData\\stock_st.feather"
df_res.to_feather(tofile)